<?php

namespace Mem;

use MemLib\MemBase;
use MemLib\Pdb;

/**
 * 支付订单表
 *
 * @version 1.0
 * @author 张云江
 */
class MemOrder_info extends MemBase
{
    public function __construct()
    {
        parent::__construct(Pdb::db_order, "t_order_info");
    }

    /**
     * 插入数据
     * @param mixed $product_id 产品ID
     * @param mixed $transaction_id 订单编号
     * @param mixed $purchase_date 购买时间
     * @param mixed $source_type 来源 1.苹果正式环境 0.苹果测试环境
     * @param mixed $expires_date 会员到期时间
     */
    public function insertDataWithParam($product_id, $application_id, $transaction_id, $purchase_date, $expires_date, $source_type)
    {
        $source_type = (int)$source_type;
        $insert_dt = date("Y-m-d H:i:s", time());
        $data = array(
            "product_id" => $product_id,
            "application_id" => $application_id,
            "transaction_id" => $transaction_id,
            "purchase_date" => $purchase_date,
            "expires_date" => $expires_date,
            "source_type" => $source_type,
            "insert_dt" => $insert_dt
        );
        $res = $this->addData($data);
        if ($res) {
            \Mlog::info("t_order_info 插入数据成功" . $transaction_id);
        }
        return $this->getTmpID();
    }

    /**
     * 根据订单详情
     * @param double $order_id
     * @return array
     */
    public function getInfoWithOrder_id($order_id)
    {
        $order_id = (float)$order_id;
        $sql = "SELECT * FROM t_order_info WHERE order_id='{$order_id}';";
        $data = $this->getData($sql, "getInfoWithOrder_id:{$order_id}");
        return $data ? $data[0] : array();
    }

    /**
     * 根据订单详情
     * @param double $transaction_id
     * @return array
     */
    public function getInfoWithtransaction_id($transaction_id)
    {
        $sql = "SELECT * FROM t_order_info WHERE transaction_id='{$transaction_id}';";
        $data = $this->getData($sql, "getInfoWithtransaction_id:{$transaction_id}");
        return $data ? $data[0] : array();
    }

    /**
     * 获取应用详情
     * @return array
     */
    public function getCountDataWithAll($application_id, $type, $stxt, $begin_dt, $end_dt)
    {
        $whereStr = '';
        if ($application_id > 0) {
            $whereStr = $whereStr . ' AND application_id = ' . $application_id;
        }
        if (!empty($stxt)) {
            if ($type == 1) {
                $whereStr = $whereStr . " AND transaction_id = '" . $stxt . "'";
            } else {
                $whereStr = $whereStr . " AND product_id = '" . $stxt . "'";
            }
        }
        if (!empty($begin_dt) && !empty($end_dt)) {
            $whereStr = " AND purchase_date > '" . $begin_dt . "' AND purchase_date < '" . $end_dt . "'";
        }
        $sql = "SELECT COUNT(*) AS total FROM t_order_info WHERE order_id != 0 {$whereStr};";
        $data = $this->getData($sql, "getCountDataWithAllapplication_id{$application_id}:type:{$type}:stxt:{$stxt}:begin_dt:{$begin_dt}end_dt:{$end_dt}");
        return empty($data) ? $data : $data[0]['total'];
    }

    /**
     * 获取应用详情
     * @return array
     */
    public function getDataWithAll($application_id, $type, $stxt, $begin_dt, $end_dt, $mempage)
    {
        $whereStr = '';
        if ($application_id > 0) {
            $whereStr = $whereStr . ' AND pcl.application_id = ' . $application_id;
        }
        if (!empty($stxt)) {
            if ($type == 1) {
                $whereStr = $whereStr . " AND pcl.transaction_id = '" . $stxt . "'";
            } else {
                $whereStr = $whereStr . " AND pcl.product_id = '" . $stxt . "'";
            }
        }
        if (!empty($begin_dt) && !empty($end_dt)) {
            $whereStr = " AND pcl.purchase_date > '" . $begin_dt . "' AND pcl.purchase_date < '" . $end_dt . "'";
        }
        $sql = "SELECT pcl.*,pclm.application_name,pcld.pay FROM db_order.t_order_info AS pcl LEFT JOIN db_order.t_order_product_info AS pcld ON pcld.product_channel = pcl.product_id LEFT JOIN db_order.t_order_application_info AS pclm ON pclm.application_id = pcl.application_id WHERE pcl.order_id != 0 {$whereStr} ORDER BY pcl.purchase_date DESC LIMIT {$mempage->getLimitIndex()},{$mempage->pagesize};";
        $data = $this->getData($sql, "getDataWithAllapplication_id{$application_id}:type:{$type}:stxt:{$stxt}:begin_dt:{$begin_dt}end_dt:{$end_dt}:AndPage:{$mempage->getLimitIndex()}:pagesize:{$mempage->pagesize}");
        return $data;
    }
}
